package com.yozinga.fragment;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.yozinga.OfferDealPage;
import com.yozinga.R;
import com.yozinga.adapter.OffersAdapter;
import com.yozinga.entity.Offers;
import com.yozinga.model.BaseModel.OnRequestFinishedCallback;

public class OffersFragment extends ListFragment implements OnRequestFinishedCallback{
	//OfferModel offersModel;
	Bundle bundle ;
	String merchantId;
	ArrayList<Offers> mOfferList;
	String merchantName;
	
	ArrayList<Offers> mOffersList;
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		bundle = this.getArguments();
		merchantId =  bundle.getString("merchantId");
		merchantName = bundle.getString("merchantName");
		Log.i("Today","Merchant Tage line isn OffersFragment"+bundle.getString("merchantTagLine"));
		Log.i("Today","merchantId is "+merchantId);
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_offers, container,false);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		//((TextView)getView().findViewById(R.id.screenName)).setText(bundle.getString("screenName"));
		//ToolKit.startProgress(getActivity(),R.string.loading);
		//offersModel = new OfferModel(getActivity());
		//offersModel.getOfferItems(AppData.OFFERS_LIST,merchantId);
		//offersModel.addOnServiceRequestCompletedListener(this);
		//mOffersList = (ArrayList<Offers>) bundle.getSerializable("offersList");

		Log.i("Today","OfferList size is: "+CategoryListFragment.offerList.size());
		
		mOfferList = CategoryListFragment.offerList;
		ArrayList<String> imagesList = new ArrayList<String>();
		for (int i = 0; i < mOfferList.size(); i++) {
			Offers offers = CategoryListFragment.offerList.get(i);
			imagesList.add(offers.getThumbnailImage());
		}
		OffersAdapter ca = new OffersAdapter(mOfferList,imagesList,getActivity());
		setListAdapter(ca);
	}

	/*@Override
	public void onPause() {
		super.onPause();
		offersModel.removeOnServiceRequestCompleteListener(this);
	}*/

	@Override
	public void onRequestFinished(int resultCode, Bundle payload) {

		//ToolKit.stopprogess();
		//String response = payload.getString("ServiceResponse");
		//Log.i("Today","Restaurant Respons is :"+payload.getString("ServiceResponse"));
		//OffersProcessor offersProcessor = new OffersProcessor();
		//list = offersProcessor.getList(response);
		ArrayList<String> mLogoList = new ArrayList<String>();
		mLogoList.add("http://www.google.co.in/imgres?imgurl=http://4.bp.blogspot.com/-8v_k_fOcfP8/UQIL4ufghBI/AAAAAAAAEDo/9ffRRTM9AnA/s400/android-robog-alone.png&imgrefurl=http://about-google-android.blogspot.com/2013/01/rumors-on-new-coming-expected-android.html&h=750&w=1160&sz=488&tbnid=RhMVr2gPztn0aM:&tbnh=89&tbnw=137&prev=/search%3Fq%3Dandroid%2Bimages%26tbm%3Disch%26tbo%3Du&zoom=1&q=android+images&usg=__zYdPt3VI4l996udAqLXqgsz7X2o=&docid=5FGZK-n-4XZ08M&sa=X&ei=9UjrUauXFM7HrQfHuIHIBA&sqi=2&ved=0CE4Q9QEwCw&dur=1510#imgdii=RhMVr2gPztn0aM%3A%3BZinz8J9QQm5J5M%3BRhMVr2gPztn0aM%3A");
		mLogoList.add("http://www.google.co.in/imgres?imgurl=http%3A%2F%2F4.bp.blogspot.com%2F-8v_k_fOcfP8%2FUQIL4ufghBI%2FAAAAAAAAEDo%2F9ffRRTM9AnA%2Fs400%2Fandroid-robog-alone.png&imgrefurl=http%3A%2F%2Fabout-google-android.blogspot.com%2F2013%2F01%2Frumors-on-new-coming-expected-android.html&docid=5FGZK-n-4XZ08M&tbnid=RhMVr2gPztn0aM%3A&w=1160&h=750&ei=FknrUYr1GcuHrgfCjYDADA&ved=0CAIQxiAwAA&iact=c");
		mLogoList.add("http://www.google.co.in/imgres?imgurl=http%3A%2F%2Fwww.androidblog.com.au%2Fwp-content%2Fuploads%2F2010%2F09%2FAndy-The-Android.jpg&imgrefurl=http%3A%2F%2Fwww.androidblog.com.au%2F2010%2F09%2Fandy-the-android-amigurumi-doll%2F&docid=_VO12_0Zt7EDIM&tbnid=1zblr6zqjYMS8M&w=1200&h=800&ei=FknrUYr1GcuHrgfCjYDADA&ved=0CAkQxiAwBw&iact=c");
		mLogoList.add("http://www.google.co.in/imgres?imgurl=http%3A%2F%2Fwww.androidblog.com.au%2Fwp-content%2Fuploads%2F2010%2F09%2FAndy-The-Android.jpg&imgrefurl=http%3A%2F%2Fwww.androidblog.com.au%2F2010%2F09%2Fandy-the-android-amigurumi-doll%2F&docid=_VO12_0Zt7EDIM&tbnid=1zblr6zqjYMS8M&w=1200&h=800&ei=FknrUYr1GcuHrgfCjYDADA&ved=0CAkQxiAwBw&iact=c");

	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);

		Offers offers = (Offers) mOfferList.get(position);
		String dealDesc = offers.getDealDesc();
		String dealName = offers.getDealName();
		String dealLogo = offers.getImageLogoUrl();
		String offerStart = offers.getOfferStart();
		String offerEnd = offers.getOfferEnd();
		String offLimitations = offers.getOfferLimitations();
		String offTagLine = offers.getOfferTagLine();
		Intent merchantIntent = new Intent(getActivity(),OfferDealPage.class);
		bundle.putString("dealName",dealName);
		bundle.putString("dealDesc",dealDesc);
		bundle.putString("dealLogo",dealLogo);
		bundle.putString("offerStart",offerStart);
		bundle.putString("offerEnd",offerEnd);
		bundle.putBoolean("favouriteOffer1", false);
		bundle.putString("offLimitations",offLimitations);
		bundle.putString("offTagLine",offTagLine);
		bundle.putString("merchantName", merchantName);
		bundle.putString("offerId", offers.getOfferId());
		merchantIntent.putExtra("merchantInfo", bundle);
		startActivity(merchantIntent);
	}

}
